package com.wyh.controller;


import com.easemob.im.server.EMService;
import com.wyh.common.annotation.Log;
import com.wyh.common.annotation.RateLimiter;
import com.wyh.common.enums.BusinessType;
import com.wyh.common.response.PageInfo;
import com.wyh.common.result.R;
import com.wyh.common.util.ExcelUtils;
import com.wyh.entity.AdminUser;
import com.wyh.request.AdminUserRequest;
import com.wyh.request.PasswordLoginRequest;
import com.wyh.request.VerifyCodeLoginRequest;
import com.wyh.service.IAdminUserService;
import com.wyh.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 用户管理控制层
 *
 * @author WangYuHang
 * @date 2024-03-03
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "用户管理接口管理",tags = "用户管理接口管理")
@RequestMapping("/admin/adminUser" )
public class AdminUserController {

    private final IAdminUserService iAdminUserService;



    /**
     * 用户密码登录
     */
    @Log(title = "用户密码登录", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "用户密码登录")
    @PostMapping("/passwordLogin")
    public R<LoginVo> passwordLogin(@RequestBody PasswordLoginRequest passwordLoginRequest) {
        R<LoginVo> loginVoR = iAdminUserService.passwordLogin(passwordLoginRequest);
        return loginVoR;
    }


    /**
     * 用户验证码登录
     */
    @Log(title = "用户验证码登录", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "用户验证码登录")
    @PostMapping("/login")
    public R<LoginVo> login(@RequestBody VerifyCodeLoginRequest verifyCodeLoginRequest) {
        return null;
    }




    /**
     * 注册用户管理
     */
    @Log(title = "注册用户", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")
    public R<Void> add(@RequestBody AdminUserRequest adminUserRequest) {
        R<Void> r = iAdminUserService.add(adminUserRequest);
        return r;
    }


    /**
     * 分页查询用户管理列表
     */
    @ApiOperation(value = "分页查询用户管理列表")
    @PostMapping("/pageList")
    public R<PageInfo<AdminUser>> pageList(@RequestBody AdminUserRequest adminUserRequest) {
        return R.success(iAdminUserService.pageList(adminUserRequest));
    }

    /**
     * 获取用户管理详细信息
     */
    @ApiOperation(value = "获取用户管理详细信息")
    @GetMapping(value = "/query/{id}")
    public R<AdminUser> detail(@PathVariable("id") String id) {
        return R.success(iAdminUserService.getById(id));
    }



    /**
     * 修改用户管理
     */
    @Log(title = "修改用户管理", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户管理")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody AdminUserRequest adminUserRequest) {
        iAdminUserService.update(adminUserRequest);
        return R.success();
    }

    /**
     * 删除用户管理
     */
    @Log(title = "删除用户管理", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除用户管理")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Integer[] ids) {
        iAdminUserService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出用户管理
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody AdminUserRequest adminUserRequest) throws IOException {
        List<AdminUser> list = iAdminUserService.list(adminUserRequest);
        ExcelUtils.exportExcel(list, AdminUser.class, "用户管理信息", response);
    }
}
